using Quiki.ExpressionParsers;
using Quiki.Templates;

namespace Quiki.ElementFormatters
{
    public class TemplateFormatter : ElementFormatterBase
    {
        public TemplateFormatter(TemplatesRegistry templatesRegistry)
        {
            this.templatesRegistry = templatesRegistry;
        }

        public override bool FormatBegin(WikiTextElement element, HtmlFormatterContext context)
        {
            if (element.ElementType != WikiTextElementType.Template)
                return false;

            WikiTemplateElement templateElement = (WikiTemplateElement) element;
            if (false == templatesRegistry.HasTemplate(templateElement.TemplateName))
                return true;

            IWikiTemplate template = templatesRegistry.GetTemplate(templateElement.TemplateName);
            template.Format(templateElement, context);

            return true;
        }

        private readonly TemplatesRegistry templatesRegistry;
    }
}